<template>
  <div class="game-script">
    <div class="header">
      <button class="back-btn" @click="goBack">← 返回</button>
      <h2>{{ scriptName }}</h2>
      <div class="header-right"></div>
    </div>

    <div class="game-content">
      <div v-if="loading" class="loading">加载中...</div>

      <div v-else>
        <!-- 剧本背景 -->
        <div class="script-background" v-if="background">
          <h3>剧本背景</h3>
          <div class="background-content">
            <p class="story">{{ background.story }}</p>
            <div class="timeline" v-if="background.timeline">
              <h4>时间线</h4>
              <ul>
                <li v-for="(event, index) in background.timeline" :key="index">{{ event }}</li>
              </ul>
            </div>
            <div class="victim-info" v-if="background.victim">
              <h4>受害者信息</h4>
              <div class="victim-details">
                <p><strong>姓名:</strong> {{ background.victim.name }}</p>
                <p><strong>年龄:</strong> {{ background.victim.age }}</p>
                <p><strong>死因:</strong> {{ background.victim.cause }}</p>
                <p><strong>死亡时间:</strong> {{ background.victim.time }}</p>
              </div>
            </div>
          </div>
        </div>

        <!-- 我的角色信息 -->
        <div class="my-role" v-if="myRoleDetails">
          <h3>我的角色</h3>
          <div class="role-card">
            <div class="role-header">
              <h4>{{ myRoleDetails.name }}</h4>
              <span class="role-basic">{{ myRoleDetails.gender }} · {{ myRoleDetails.age }}岁</span>
            </div>
            <div class="role-content">
              <div class="role-section" v-if="myRoleDetails.background">
                <h5>背景故事</h5>
                <p>{{ myRoleDetails.background }}</p>
              </div>
              <div class="role-section" v-if="myRoleDetails.secret">
                <h5>我的秘密</h5>
                <p class="secret">{{ myRoleDetails.secret }}</p>
              </div>
              <div class="role-section" v-if="myRoleDetails.task">
                <h5>我的任务</h5>
                <p class="task">{{ myRoleDetails.task }}</p>
              </div>
              <div class="role-section" v-if="myRoleDetails.clues && myRoleDetails.clues.length > 0">
                <h5>我的线索</h5>
                <ul class="clues-list">
                  <li v-for="(clue, index) in myRoleDetails.clues" :key="index">{{ clue }}</li>
                </ul>
              </div>
              <div class="role-section" v-if="myRoleDetails.relationship">
                <h5>角色关系</h5>
                <div class="relationship-list">
                  <div v-for="(relation, name) in myRoleDetails.relationship" :key="name" class="relationship-item">
                    <strong>{{ name }}:</strong> {{ relation }}
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 玩家列表 -->
        <div class="players-info">
          <h3>玩家列表</h3>
          <div class="players-list">
            <div v-for="player in players" :key="player.id" class="player-item">
              <div class="player-avatar">{{ player.role[0] }}</div>
              <div class="player-info">
                <div class="player-name">{{ player.name }}</div>
                <div class="player-role">{{ player.role }}</div>
              </div>
            </div>
          </div>
        </div>

        <!-- 游戏操作 -->
        <div class="game-actions">
          <button class="btn btn-primary" @click="viewClues">查看线索</button>
          <button class="btn btn-secondary" @click="viewDiscussion">进入讨论</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, onUnmounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import axios from "axios";

export default {
  name: "GameScript",
  setup() {
    const router = useRouter();
    const route = useRoute();
    const roomId = route.params.id;

    const loading = ref(true);
    const scriptName = ref("剧本");
    const background = ref(null);
    const myRoleDetails = ref(null);
    const players = ref([]);
    const scriptId = ref(null);

    let pollInterval = null;

    const loadGameInfo = async () => {
      try {
        // 获取房间信息
        const roomResponse = await axios.get(`/api/rooms/${roomId}`);
        if (roomResponse.data.success) {
          const room = roomResponse.data.room;
          players.value = room.players || [];
          scriptId.value = room.script || "script1";
          
          // 获取剧本信息
          const scriptResponse = await axios.get(`/api/scripts/${scriptId.value}`);
          if (scriptResponse.data.success) {
            const script = scriptResponse.data.script;
            scriptName.value = script.name;
            background.value = script.background;

            // 获取当前玩家的角色（简化处理：使用第一个玩家，实际应该从session获取）
            // 如果玩家列表不为空，获取第一个玩家的角色信息
            if (room.players && room.players.length > 0) {
              // 简化：使用第一个玩家，实际应该根据session获取当前玩家
              const myPlayer = room.players[0];
              if (myPlayer && myPlayer.roleId) {
                const roleResponse = await axios.get(
                  `/api/scripts/${scriptId.value}/roles/${myPlayer.roleId}`
                );
                if (roleResponse.data.success) {
                  myRoleDetails.value = roleResponse.data.role;
                }
              }
            }
          }
        }
      } catch (error) {
        console.error("加载游戏信息失败:", error);
      } finally {
        loading.value = false;
      }
    };

    const viewClues = () => {
      // TODO: 跳转到线索页面
      alert("线索功能开发中...");
    };

    const viewDiscussion = () => {
      // TODO: 跳转到讨论页面
      alert("讨论功能开发中...");
    };

    const goBack = () => {
      router.push(`/room/${roomId}`);
    };

    onMounted(() => {
      loadGameInfo();
      // 轮询更新游戏信息
      pollInterval = setInterval(loadGameInfo, 5000);
    });

    onUnmounted(() => {
      if (pollInterval) {
        clearInterval(pollInterval);
      }
    });

    return {
      loading,
      scriptName,
      background,
      myRoleDetails,
      players,
      viewClues,
      viewDiscussion,
      goBack,
    };
  },
};
</script>

<style scoped>
.game-script {
  min-height: 100vh;
  background: #f5f5f5;
}

.header {
  background: white;
  padding: 15px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header h2 {
  font-size: 20px;
  font-weight: 600;
}

.back-btn {
  background: none;
  border: none;
  color: #667eea;
  font-size: 16px;
  cursor: pointer;
  padding: 5px 10px;
}

.header-right {
  width: 60px;
}

.game-content {
  padding: 20px;
}

.loading {
  text-align: center;
  padding: 60px 20px;
  color: #999;
}

.script-background,
.my-role,
.players-info {
  background: white;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.script-background h3,
.my-role h3,
.players-info h3 {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 15px;
}

.background-content {
  font-size: 14px;
  line-height: 1.8;
}

.story {
  margin-bottom: 20px;
  color: #333;
}

.timeline {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #f0f0f0;
}

.timeline h4 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 10px;
  color: #667eea;
}

.timeline ul {
  list-style: none;
  padding: 0;
}

.timeline li {
  padding: 8px 0;
  padding-left: 20px;
  position: relative;
}

.timeline li:before {
  content: "•";
  position: absolute;
  left: 0;
  color: #667eea;
  font-size: 20px;
}

.victim-info {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #f0f0f0;
}

.victim-info h4 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 10px;
  color: #e74c3c;
}

.victim-details p {
  margin: 8px 0;
  font-size: 14px;
}

.role-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 12px;
  padding: 20px;
  margin-top: 15px;
}

.role-header {
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.3);
}

.role-header h4 {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 5px;
}

.role-basic {
  font-size: 14px;
  opacity: 0.9;
}

.role-content {
  font-size: 14px;
  line-height: 1.8;
}

.role-section {
  margin-bottom: 20px;
}

.role-section:last-child {
  margin-bottom: 0;
}

.role-section h5 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 10px;
  color: rgba(255, 255, 255, 0.95);
}

.role-section p {
  margin: 0;
  white-space: pre-line;
}

.secret {
  background: rgba(255, 255, 255, 0.1);
  padding: 12px;
  border-radius: 8px;
  border-left: 3px solid #ffd700;
}

.task {
  background: rgba(255, 255, 255, 0.1);
  padding: 12px;
  border-radius: 8px;
  border-left: 3px solid #4caf50;
}

.clues-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.clues-list li {
  padding: 8px 12px;
  margin-bottom: 8px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  border-left: 3px solid #2196f3;
}

.relationship-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.relationship-item {
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 8px;
}

.relationship-item strong {
  color: #ffd700;
}

.players-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 15px;
}

.player-item {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 12px;
  background: #f9f9f9;
  border-radius: 8px;
}

.player-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 600;
}

.player-info {
  flex: 1;
}

.player-name {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 4px;
}

.player-role {
  font-size: 12px;
  color: #999;
}

.game-actions {
  display: flex;
  flex-direction: column;
  gap: 15px;
  margin-top: 20px;
}

.btn {
  padding: 16px 32px;
  font-size: 16px;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  font-weight: 600;
  transition: all 0.3s;
}

.btn-primary {
  background: #667eea;
  color: white;
}

.btn-primary:active {
  background: #5568d3;
  transform: scale(0.98);
}

.btn-secondary {
  background: #764ba2;
  color: white;
}

.btn-secondary:active {
  background: #653a8f;
  transform: scale(0.98);
}
</style>

